<template>
  <div class="s-print">
    <div id="print"></div>
  </div>
</template>

<script>
  import adviceSettlement from './print/adviceSettlement.vue';
  import borrowing from './print/borrowing.vue';
  import expenseCost from './print/expenseCost.vue';
  import rewardDeduction from './print/rewardDeduction.vue';
  import travelExpense from './print/travelExpense.vue';
  import {GetLodop} from '~/plugins/Tools';
  import Vue from 'vue';
  import XLSX from 'xlsx';
  function print_real (list) {
    let LODOP = GetLodop();
    LODOP.PRINT_INIT('打印:报销单');
    const Height = 1123;
    let height = 0;
    list.forEach(({info, item}, index) => {
      if (!(info && info.code)) {
        console.error('打印 info 必须具有明确的 code 值！ ' + index);
      }
      let print = {};
      let intOrient = 0;
      switch (info.code) {
      case 'adviceSettlement':
        intOrient = 2;
        print = adviceSettlement;
        break;
      case 'borrowing':
        print = borrowing;
        break;
      case 'expenseCost':
        print = expenseCost;
        break;
      case 'rewardDeduction':
        print = rewardDeduction;
        break;
      case 'travelExpense':
        print = travelExpense;
        break;
      }
      LODOP.SET_PRINT_PAGESIZE(intOrient, 0, 0, 'A4');
      // this.dialogPrint.visible = true;
      // let time = this.formatterTime(new Date(), 6);
      // let index = 1;
      let printf = new Vue({
        el: '#print',
        render (h) {
          return h(
            'print', {
              props: {
                data: item,
              },
            }
          );
        },
        components: {
          print,
        },
      });
      // index++;
      // console.log(printf);
      let HTML = `<html>
${printf.$children[0].css}
<body>
${printf.$el.innerHTML}
</body>
</html>`;
      // console.log(HTML);
      height += printf.$el.offsetHeight;
      if (info.code === 'adviceSettlement' || height > Height) {
        LODOP.NewPage();
        height = printf.$el.offsetHeight;
      }
      LODOP.ADD_PRINT_HTM(height - printf.$el.offsetHeight, 0, '100%', '100%', HTML);
    });
    LODOP.PREVIEW();
  }
  function print_electronic (list) {
    let wb = {
      SheetNames: [],
      Sheets: {},
    };
    list.forEach(({info, item}, index) => {
      if (!(info && info.code)) {
        console.error('打印 info 必须具有明确的 code 值！ ' + index);
      }
      let print = {};
      switch (info.code) {
      case 'adviceSettlement':
        print = adviceSettlement;
        break;
      case 'borrowing':
        print = borrowing;
        break;
      case 'expenseCost':
        print = expenseCost;
        break;
      case 'rewardDeduction':
        print = rewardDeduction;
        break;
      case 'travelExpense':
        print = travelExpense;
        break;
      }
      let printf = new Vue({
        el: '#print',
        render (h) {
          return h(
            'print', {
              props: {
                data: item,
              },
            }
          );
        },
        components: {
          print,
        },
      });
      let {SheetNames, Sheets} = XLSX.utils.table_to_book(printf.$el.querySelector('#report1'), {
        sheet: info.title + '   ' + item.reimbursementCode,
        raw: true,
      });
      wb.SheetNames = wb.SheetNames.concat(SheetNames);
      wb.Sheets = Object.assign(wb.Sheets, Sheets);
      // console.log(item);
    });
    XLSX.writeFile(wb, (new Date()).toLocaleString() + '电子版.xlsx');
  }
  export default {
    methods: {
      print (list = [], type = 'real') {
        if (list.constructor !== Array) {
          list = [list];
        }
        switch (type) {
        case 'real':
          print_real(list);
          break;
        case 'electronic':
          print_electronic(list);
          break;
        }
      },
    },
  };
</script>

<style>
  .s-print {
    position: absolute;
    top: 0;
    left: 0;
    opacity: 0;
    z-index: -1;
  }
</style>
